﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI.Common
{
    public class AddTab
    {
        public static List<string> Permission { get; set; } = new List<string>() { };
        public static Action<string> UpdatePermissioned;
        //public UpdatePermissioned updatePermissioned { get; set; }
        public AddTab()
        {   
        }
        public AddTab(Form frm, string tabtext)
        {
            if (!checkPermission(tabtext)) { MessageBox.Show("无访问权限！"); return; };
            frm.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="frm">添加的窗体</param>
        /// <param name="tabname">tabpage页名</param>
        /// <param name="tabtext">tabpage页显示名</param>
        /// <param name="tabcontrol">tabcontrol控件</param>
        public AddTab(Form frm, string tabname, string tabtext, TabControl tabcontrol)
        {
            if (!checkPermission(tabtext)) { MessageBox.Show("无访问权限！"); return; };
            bool hastab = false;
            ///一下是添加一个tabpage的逻辑，无则添加锁定，有则直接锁定
            foreach (TabPage tab in tabcontrol.TabPages)
            {
                if (tab.Name == tabname && tab.Text == tabtext)
                {
                    hastab = true;
                    if (!tabname.Contains("Search"))
                        tabcontrol.SelectedTab = tab;
                    break;
                }
            }
            if (!hastab || tabname.Contains("Search"))
            {
                frm.FormBorderStyle = FormBorderStyle.None;
                frm.BackColor = Color.LightBlue;
                frm.TopLevel = false;

                frm.AutoSize = true;
                TabPage page = new TabPage();
                page.Name = tabname;
                page.Text = tabtext;
                page.BackColor = Color.LightBlue;
                page.Controls.Add(frm);
                tabcontrol.TabPages.Add(page);
                tabcontrol.SelectedTab = page;
                frm.Show();
            }
        }

        private bool checkPermission(string tabtext)
        {
            UpdatePermissioned(tabtext);
            if (Permission.Contains(tabtext))
                return true;
            return false;
        }

        public AddTab(UserControl control, string tabname, string tabtext, TabControl tabcontrol)
        {
            if (!checkPermission(tabtext)) { MessageBox.Show("无访问权限！"); return; };
            bool hastab = false;
            ///一下是添加一个tabpage的逻辑，无则添加锁定，有则直接锁定
            foreach (TabPage tab in tabcontrol.TabPages)
            {
                if (tab.Name == tabname && tab.Text == tabtext)
                {
                    hastab = true;
                    if (!tabname.Contains("Search"))
                        tabcontrol.SelectedTab = tab;
                    break;
                }
            }
            if (!hastab || tabname.Contains("Search"))
            {
                control.BackColor = tabcontrol.TabPages[0].BackColor;
                //control.BackColor =  Color.LightBlue;
                control.Dock = DockStyle.Fill;

                TabPage page = new TabPage();
                page.Name = tabname;
                page.Text = tabtext;
                //page.BackColor = Color.LightBlue;
                page.Controls.Add(control);
                tabcontrol.TabPages.Add(page);
                tabcontrol.SelectedTab = page;
                control.Show();
            }
        }
    }
}
